//
//  LTSetViewController.m
//  LTChat
//
//  Created by 俊松何 on 2017/7/28.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTSetViewController.h"
#import "LTAboutController.h"
#import "LTFeedbackController.h"
//#import "LTLoginViewController.h"
#import "JPUSHService.h"
//#import "LTShieldSettingVC.h"
#import "YTKKeyValueStore.h"
#import "MTLoginFristController.h"
#import "MTUserShildTableController.h"
#import "MTLiveManagerListController.h"

@interface LTSetViewController ()
@property (weak, nonatomic) IBOutlet UILabel *squreLable;
@property (weak, nonatomic) IBOutlet UILabel *aboutLable;
@property (weak, nonatomic) IBOutlet UILabel *clearLable;
@property (weak, nonatomic) IBOutlet UILabel *questionLable;

@property (weak, nonatomic) IBOutlet UIButton *logoutButton;

@end

@implementation LTSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLTLocalizedString(@"set.title", nil);
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    
//    self.squreLable.text = @"用户屏蔽";// NSLTLocalizedString(@"set.squreset", nil);
    self.aboutLable.text = [NSString stringWithFormat:NSLTLocalizedString(@"set.aboutlinktalk", nil),app_Name];
    self.clearLable.text = NSLTLocalizedString(@"set.clearmemcache", nil);
    self.questionLable.text = NSLTLocalizedString(@"set.question", nil);
    [self.logoutButton setTitle:NSLTLocalizedString(@"set.logout", nil) forState:UIControlStateNormal];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   return 55;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{
            MTUserShildTableController *shildUserVc = [[MTUserShildTableController alloc] init];
            [self.navigationController pushViewController:shildUserVc animated:YES];
        }break;
        case 1:{
//            MTWorksShildViewController *shildWorkVc = [[MTWorksShildViewController alloc] init];
//            [self.navigationController pushViewController:shildWorkVc animated:YES];
        }break;
        case 2:{
            LTAboutController *VC = [[LTAboutController alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
        }break;
        case 3:{
            // 移除朋友圈缓存
            YTKKeyValueStore * store = [[YTKKeyValueStore alloc] initDBWithName:@"friendsCircle.db"];
            [store createTableWithName:StoreTableName];
            [store clearTable:StoreTableName];
            
            for (int i = 1; i < 6; i++) {
                NSString *storeTableName = [NSString stringWithFormat:@"%@%d", SquareStoreTableName, i];
                [store createTableWithName:storeTableName];
                [store clearTable:storeTableName];
            }
            [MBProgressHUD showSuccess:@"清除完成" toView:self.view];
        }break;
        case 4:{
            LTFeedbackController *VC = [[LTFeedbackController alloc] initWithNibName:@"LTFeedbackController" bundle:nil];
            [self.navigationController pushViewController:VC animated:YES];
        }break;
        case 5:{
            [[MTUserApiManager manager] getUserAuthenticationinfoWithcompletion:^(UserAuthenticationinfo *info, MTError *mterror) {
                if(mterror == nil)
                {
                    MTWebViewController *webVc = [[MTWebViewController alloc] init];
                    webVc.url = info.authenticationUrl;
                    webVc.isShow = YES;
                    [self.navigationController pushViewController:webVc animated:YES];
                }
            }];
        }break;
        case 6:{
            MTLiveManagerListController *managerVc = [[MTLiveManagerListController alloc] init];
            managerVc.type = 1;
            managerVc.liveUserId = [LTClient sharedClient].currentUserId;
            [self.navigationController pushViewController:managerVc animated:true];
        }break;
            
        default:
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (IBAction)loginOut:(id)sender {
    
    UIAlertController *tipAlert = [UIAlertController alertControllerWithTitle:nil message:NSLTLocalizedString(@"set.sureLogout", nil) preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.cancle", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        MTLoginFristController *fristVc = [MTLoginFristController baseStoryBordWithName:@"Login"];
        MTNavigationController *mainVC = [[MTNavigationController alloc] initWithRootViewController:fristVc];
        [[LTClient sharedClient] loginOut];
        [MTUserModel logout];
        
        // 移除朋友圈缓存
        YTKKeyValueStore * store = [[YTKKeyValueStore alloc] initDBWithName:@"friendsCircle.db"];
        [store createTableWithName:StoreTableName];
        [store clearTable:StoreTableName];
        for (int i = 1; i < 6; i++) {
            NSString *storeTableName = [NSString stringWithFormat:@"%@%d", SquareStoreTableName, i];
            [store createTableWithName:storeTableName];
            [store clearTable:storeTableName];
        }
        // 联系人数
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:CotanctsNum];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
        [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
        
        
    }];
    [tipAlert addAction:action1];
    [tipAlert addAction:action2];
    
    [self presentViewController:tipAlert animated:YES completion:^{
        
    }];
    
    
  
}




@end
